package com.throttle.raildroid;
import java.io.BufferedReader;
	import java.io.IOException;
	import java.io.InputStreamReader;
	import java.net.Socket;
	
public class mainhelpclass {
	private class msgReader implements Runnable{

		InputStreamReader in;
		msgReader(Socket sock){
			try {
				in = new InputStreamReader(sock.getInputStream());
			} catch (IOException e) {
				System.out.println("no input stream");
				e.printStackTrace();
			}
		}
		
		//@Override
		public void run() {
			while(true){
				try {
					if(in.ready()){
						System.out.print("Input recieved: ");
						while(in.ready()){
							int size = in.read();
							assert size >= 0;
							if(size > 0){
								byte[] inmsg = new byte[size+2];
								inmsg[0] = (byte)size;
								inmsg[1] = (byte)in.read();
								for(int i=2; i<size+2; i++)
									inmsg[i] = (byte)in.read();
								System.out.println(MessageLibrary.decrypt(inmsg));
							}
							//System.out.print(in.read()+" ");
						}
						System.out.println("");
					}
				} catch (IOException e) {
					System.out.println("Input Exception");
					e.printStackTrace();
				}//*/
			}
		}
	}
	
	byte[] msg1 = new byte[6];
    boolean bell;
    boolean horn;
    boolean light;
    boolean dir;
    Socket mysocket;
    
	public mainhelpclass(){
		bell = false;
		horn  = false;
		light  = false;
		dir  = false;
		mysocket = MessageLibrary.connect("127.0.0.1", 14804);
	}
	
	public void dothis(){
		
	     try {
        	String userInput = "";
        	BufferedReader stdIn = new BufferedReader(
                    new InputStreamReader(System.in));
        	
        	System.out.print("Enter which train to take: ");
        	userInput = stdIn.readLine();
        	int  tr = Integer.parseInt(userInput);
        	
        	//mysocket = MessageLibrary.connect("127.0.0.1", 14804);
        	
        	
        	
        	mainhelpclass.msgReader m = this.new msgReader(mysocket);
        	new Thread(m).start();
        	//t.start();
        	
        	if(!MessageLibrary.controlTrain(tr))
        		System.out.println("Connection not set");
        	System.out.print("Enter Command (setspeed, changesw, setmisc). 'end' to end: ");
        	while(!(userInput = stdIn.readLine()).equals("end")){
        		if(userInput.equals("setspeed")){
        			System.out.print("Enter Speed: ");
        			userInput = stdIn.readLine();
        			int sp = Integer.parseInt(userInput);
        			if(!MessageLibrary.setSpeed(1, sp))
        				System.out.println("Connection not set");
        		}else if(userInput.equals("changesw")){
        			System.out.print("Enter switch to be changed 0-24: ");
        			userInput = stdIn.readLine();
        			int sw = Integer.parseInt(userInput);
        			System.out.print("Enter state (0/1): ");
        			userInput = stdIn.readLine();
        			int st = Integer.parseInt(userInput);
        			boolean ints = false;
        			if(st == 0)
        				ints = false;
        			else if(st == 1)
        				ints = true;
        			MessageLibrary.changeSwitch(sw, ints);
        		}else if(userInput.equals("setmisc")){
        			System.out.print("Enter what to change (horn/bell/mute/dir/light): ");
        			userInput = stdIn.readLine();
        			if(userInput.equals("horn")){
        				System.out.print("Which state? ");
        				userInput = stdIn.readLine();
        				int sth = Integer.parseInt(userInput);
        				if(sth == 0){
        					MessageLibrary.setMisc(1, dir, false, bell, light);
        					horn = false;
        				}else if (sth == 1){
        					MessageLibrary.setMisc(1, dir, true, bell, light);
        					horn = true;
        				}
        			}else if(userInput.equals("bell")){
        				System.out.print("Which state? ");
        				userInput = stdIn.readLine();
        				int sth = Integer.parseInt(userInput);
        				if(sth == 0){
        					MessageLibrary.setMisc(1, dir, horn, false, light);
        					bell = false;
        				}else if (sth == 1){
        					MessageLibrary.setMisc(1, dir, horn, true, light);
        					bell = true;
        				}
        			}else if(userInput.equals("light")){
        				System.out.print("Which state? ");
        				userInput = stdIn.readLine();
        				int sth = Integer.parseInt(userInput);
        				if(sth == 0){
        					MessageLibrary.setMisc(1, dir, horn, bell, false);
        					light = false;
        				}else if (sth == 1){
        					MessageLibrary.setMisc(1, dir, horn, bell, true);
        					light = true;
        				}
        			}else if(userInput.equals("dir")){
        				System.out.print("Which state? ");
        				userInput = stdIn.readLine();
        				int sth = Integer.parseInt(userInput);
        				if(sth == 0){
        					MessageLibrary.setMisc(1, false, horn, bell, light);
        					dir = false;
        				}else if (sth == 1){
        					MessageLibrary.setMisc(1, true, horn, bell, light);
        					dir = true;
        				}
        			}else if(userInput.equals("mute")){
        				System.out.print("Which state? ");
        				userInput = stdIn.readLine();
        				int sth = Integer.parseInt(userInput);
        				if(sth == 0)
        					MessageLibrary.setMute(1, false);
        				else if (sth == 1)
        					MessageLibrary.setMute(1, true);	
        			}else // end of semisc
        				System.out.println("That was not a correct command");
        	}else if(userInput.equals("control")){
        		System.out.print("Which train to control? ");
				userInput = stdIn.readLine();
				int sth = Integer.parseInt(userInput);
				MessageLibrary.controlTrain(sth);
        	}else if(true)// end of main if
        		System.out.println("That was not a correct command");
        	System.out.print("Enter Command (setspeed, changesw, setmisc). 'end' to end: ");
        }
        mysocket.close();
        
	    }catch (java.net.UnknownHostException u) {
            System.out.println("uhe!"); // Display the string.
            System.err.println(u);
        }catch (java.io.IOException e) {
            System.out.println("ioex!"); // Display the string.
            System.err.println(e);
        }
	}
}
